package com.substation.ui.activity;

import android.content.DialogInterface;
import android.support.v7.widget.DividerItemDecoration;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.greenentity.SafetyCard;
import com.greenentity.Task;
import com.interfaces.ResponseListener;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;
import com.substation.R;
import com.substation.greendao.SafetyCardDao;
import com.substation.utils.DialogUtils;
import com.substation.utils.GreenDaoManager;
import com.substation.utils.SPUtils;
import com.substation.utils.T;
import com.substation.utils.TestingUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SafetyCardActivity extends BaseListActivity {


    @BindView(R.id.count)
    TextView count;
    @BindView(R.id.score)
    TextView score;
    List<SafetyCard> datas = new ArrayList<>();
    SuperAdapter<SafetyCard> adapter;
    Task task;
    @BindView(R.id.option)
    EditText option;

    @Override
    protected int getMyListLayoutId() {
        return R.layout.activity_safety_card;
    }

    @Override
    protected void initView() {
        super.initView();
        list.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        adapter.setData(datas);
        refresh.setEnabled(false);
        task = JSON.parseObject((String) SPUtils.get(context, "task", ""), Task.class);
        option.setText(task.getSupervisorOpinion());
        datas.addAll(GreenDaoManager.getInstance().getmDaoSession().getSafetyCardDao().queryBuilder().where(SafetyCardDao.Properties.WorkGuid.eq(task.getWorkGuid())).list());
        for (SafetyCard safetyCard : datas) {
            if (safetyCard.getScore() == 0) {
                safetyCard.setScore(safetyCard.getValue());
            }
        }
        adapter.notifyDataSetChanged();
        setScore();
    }

    @Override
    public SuperAdapter getAdapter() {
        return adapter = new SuperAdapter<SafetyCard>(R.layout.content_safetycards) {
            @Override
            public void bindView(ViewHolder viewHolder, final SafetyCard safetyCard, int i) {
                ((TextView) viewHolder.getView(R.id.item)).setText(safetyCard.getSupItem());
                ((TextView) viewHolder.getView(R.id.content)).setText(safetyCard.getSupContent());
                ((TextView) viewHolder.getView(R.id.total_score)).setText(safetyCard.getValue() + "");
                final EditText editText = viewHolder.getView(R.id.score);
                if (editText.getTag() instanceof TextWatcher) {
                    editText.removeTextChangedListener((TextWatcher) editText.getTag());
                }
                editText.setText("" + safetyCard.getScore());
                TextWatcher textWatcher = new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        if (TextUtils.isEmpty(editable.toString())) {
                            safetyCard.setScore(0);
                        } else {
                            safetyCard.setScore(Integer.parseInt(editable.toString()));
                        }
                        setScore();
                    }
                };
                editText.addTextChangedListener(textWatcher);
                editText.setTag(textWatcher);
            }
        };
    }

    void setScore() {
        count.setText("监督数量:" + datas.size());
        int mScore = 0;
        for (SafetyCard safetyCard : datas) {
            mScore += safetyCard.getScore();
        }
        score.setText("总分:" + mScore);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_save, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.upload:
                DialogUtils.showEditTextDialog(context, "请("+task.getSupervisorUserIdvalue()+")监督人填写密码", new ResponseListener<String>() {
                    @Override
                    public void response(String data) {
                        if (!TestingUtils.isEmpty(context, data, "密码")) return;
                        String options = option.getText().toString();
                        if (!TestingUtils.isEmpty(context, options, "监督人意见")) return;
                        GreenDaoManager.getInstance().getmDaoSession().getSafetyCardDao().updateInTx(datas);
                        task.setPassword(data);
                        task.setSupervisorOpinion(options);
                        GreenDaoManager.getInstance().getmDaoSession().getTaskDao().update(task);
                        SPUtils.put(context, "task", JSON.toJSONString(task));
                        T.showLong(context, "保存成功");
                        finish();
                    }
                });
                return true;
            case android.R.id.home:
                DialogUtils.showMsgDialog(context, "是否返回", "您还未保存？是否直接退出？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                });
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
